package com.ybkj.o2o.mc.manager.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ybkj.o2o.mc.common.AccountUtils;
import com.ybkj.o2o.mc.common.DataGrid;
import com.ybkj.o2o.mc.common.query.DepartmentQuery;
import com.ybkj.o2o.mc.entity.Department;
import com.ybkj.o2o.mc.entity.PassengerAccount;
import com.ybkj.o2o.mc.manager.DepartmentManager;
import com.ybkj.o2o.mc.service.DepartmentService;
import com.ybkj.o2o.mc.service.PassengerAccountService;

/**
 * 
 * @author Yuan Tianyang.---2017/7/1
 *
 */
@Service
public class DefaultDepartmentManager implements DepartmentManager{
	
	@Autowired
	private DepartmentService departmentService;
	
	@Autowired
	private PassengerAccountService passengerAccountService;
	
	@Override
	public Department exists(String name, String appKey, Long enterpriseId) {
		
	    return departmentService.exists(name, appKey, enterpriseId);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public Department insert(Department department) {
		departmentService.insert(department);
		return department;
	}

	@Override
	public DataGrid<Department> findByDepartmentQuery(
			DepartmentQuery departmentQuery) {
		
		List<Department> list = departmentService.findByDepartmentQuery(departmentQuery);
		
	    Long count = departmentService.countByDepartmentQuery(departmentQuery);
	    DataGrid<Department> dataGrid = new DataGrid<>();
	    dataGrid.setRows(list);
	    dataGrid.setTotal(count);
	    return dataGrid;
	}

	@Override
	public Department findByIdAndAppKey(Long id, String appKey) {
		return departmentService.findByIdAndAppKey(id, appKey);
	}
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public int updateByPrimaryKey(Department department){
		department.setUpdated(new Date());
		return departmentService.updateByPrimaryKey(department);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int deleteByPrimaryKey(Long id, String appKey) {
		
	    return departmentService.deleteByPrimaryKey(id, appKey);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public PassengerAccount updateAccount(Department department,
			PassengerAccount account, boolean updateBlance,
			boolean useNoDrawMoney) {
		
		if (updateBlance) {
			AccountUtils.recharge(account, department, useNoDrawMoney);
			int ret2 = departmentService.updateBalance(department);
			if (ret2 != 1) {
				throw new RuntimeException("修改部门金额信息失败");
			}
		}
		account.setAfterDrawMoney(department.getDrawMoney());
		account.setAfterNoDrawMoney(department.getNodrawMoney());
		account.setCreated(new Date());
		account.setUpdated(new Date());
		account.setVersion(1);

		int ret = passengerAccountService.insert(account);
		if (ret != 1) {
			throw new RuntimeException("保存部门账户记录信息失败");
		}

		return account;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int batchDeleteByIds(List<Long> departmentIds, String appKey) {
		return departmentService.batchDeleteByIds(departmentIds,appKey);
	}

	@Override
	public int updateEnterpriseNameByEnterpriseId(Long enterpriseId, String enterpriseName, String appKey) {
		return departmentService.updateEnterpriseNameByEnterpriseId(enterpriseId,enterpriseName,appKey);
	}

	@Override
	public int updateCompanyName(Long companyId, String abbreviation,
			String appKey) {
		return departmentService.updateCompanyName(companyId, abbreviation, appKey);
	}

}
